/**
 * 测试基础的curd
 * 
 * */


//@Bean
class TestBaseCurd {

	//@Autowired
	school;

	//@Autowired
	classes;

	//@Autowired
	student;

	log;

	/**
	 * 开始测试
	 * */
	async doTest(){

		const log = this.log;

		/************************ 清空数据************************/
		//batchRemove(searchData)  searchData:匹配数据 {}代表匹配所有

		await this.student.batchRemove({});
		await this.classes.batchRemove({});
		await this.school.batchRemove({});



		/*********************增************************************/
		for(let i=0;i<10;i++){
			// false 代表快速插入的 返回插入的id 
			const {id:schoolId} = await this.school.save({name:`school${i}`,address:`address${i}`},false);
			const {id:classesId} = await this.classes.save({name:`classes${i}`,schoolId,code:i},false)

			for(let j=0;j<5+i;j++){
				/**
				 * 这里插入数据后，会将数据库的学生字段封装成学生对象返回，并且递归查询出级联对象信息 
				 * 例如查出的学生对象可以连带返回班级和学校信息
				 * （使用了1级缓存，减少与数据库的IO请求，提高性能）
				 * */
				const stu = await this.student.save({
					name:`student${j}`,
					age:10+i,
					score: (Math.random()*100).toFixed(2),
					classesId
				})

				stu.sayInfo();

			}
		}


		/*********************删************************************/


		//查询条件  学生姓名 student0 班级名称 classes0
		const stu_0_del = await this.student.findOne({name:'student0',classesId$name:'classes0'})
		const stu_1_del = await this.student.findOne({name:'student1',classesId$name:'classes0'})

		//模板单个删除
		await this.student.remove(stu_0_del.id);

		//对象自身方法删除
		await stu_1_del.$remove();

		/**
		 * 模板批量删除 把班级名称 classes0 的学生 全部删除
		 * 
		 * DELETE FROM Student WHERE id in (
		 * 	SELECT t.id FROM Student AS t \n' +
		 *   'LEFT JOIN Classes AS s0 ON t.classesId=s0.id  \n' +
		 *   'WHERE 1=1 AND s0.name=?)
		 * 
		 * */
		await this.student.batchRemove({classesId$name:'classes0'})



		/*************************改 ***************************/


		//对象自身修改
		const classesStu = await this.classes.findOne({name:'classes1'})
		classesStu.name = 'classes1_update'
		await classesStu.$update();

		/**
		 * 	模板批量修改 
		 *  batchUpdate(searchData,updateObject) searchData 条件匹配 updateObject 修改对象
		 *  将班级名称【classes1_update】的所有学生 年龄全部修改为 30
		 *  生成SQL：
		 * 	UPDATE Student SET age=? WHERE id in (
		 * 	SELECT t.id FROM Student AS t \n' +
		 *   'LEFT JOIN Classes AS s0 ON t.classesId=s0.id  \n' +
		 *   'WHERE 1=1 AND s0.name=?);
		 * */
		await this.student.batchUpdate({classesId$name:'classes1_update'},{age:30})

		/**
		 * 将学校名称为[school2]的所有学生 年龄修改为35
		 * 生成sql：
		 * UPDATE Student SET age=? WHERE id in (
		 *  SELECT t.id FROM Student AS t \n' +
		 *   'LEFT JOIN Classes AS s0 ON t.classesId=s0.id\n' +
		 *   'LEFT JOIN School AS s1 ON s0.schoolId=s1.id  \n' +
		 *   'WHERE 1=1 AND s1.name=?)
		 * */
		await this.student.batchUpdate({classesId$schoolId$name:'school2'},{age:35})



		/*************************查 ***************************/

		//单个id查询 如果不存在 返回Null
		const stu = await this.student.getById('xx')

		/**
		 *   复杂查询  => 查询所有符合条件的数据
		 * 
		 *	 SELECT t.id,t.classesId,t.name,t.age,t.score,t.date FROM Student AS t \n' +
		 *   'LEFT JOIN Classes AS s0 ON t.classesId=s0.id\n' +
		 *   'LEFT JOIN Classes AS s2 ON t.classesId=s2.id\n' +
		 *   'LEFT JOIN School AS s3 ON s2.schoolId=s3.id  \n' +
		 *   'WHERE 1=1 AND t.name LIKE ? AND t.age < ? AND s0.code < ? AND s0.name LIKE ? AND s3.name LIKE
		 * ? ;
		 * 	param: [ '%student%', 30, 30, '%7%', '%school%' ]
		 * */

		const searchData = {
			name_fuzzy:'student',
			age_lt:30,
			classesId$code_lt:30,
			classesId$name_fuzzy:'7',
			classesId$schoolId$name_fuzzy:'school'
		}

		const stus = await this.student.find(searchData)

		/**
		 *  复杂查询  => 分页查询
		 *  分页信息会赋值到paginSearch
		 * 
		 *	SELECT t.id,t.classesId,t.name,t.age,t.score,t.date FROM Student AS t \n' +
		 *   'LEFT JOIN Classes AS s0 ON t.classesId=s0.id\n' +
		 *   'LEFT JOIN Classes AS s2 ON t.classesId=s2.id\n' +
		 *   'LEFT JOIN School AS s3 ON s2.schoolId=s3.id  \n' +
		 *   'WHERE 1=1 AND t.name LIKE ? AND t.age < ? AND s0.code < ? AND s0.name LIKE ? AND s3.name LIKE ? LIMIT 10 OFFSET 0;'
		 * 	 param: [ '%student%', 30, 30, '%7%', '%school%' ]
		 *  
		 * */
		const paginSearch = {...searchData,$pageSize:10,$curPage:1};
		const stusPagin = await this.student.findPagin(paginSearch);

	}


}


module.exports = {TestBaseCurd};